home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap07 / howto04 / delphi10 / scanners / scanners.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-09-17  |  8.1 KB  |  275 lines

  1. unit Scanners;
  2.  
  3. interface
  4.  
  5. uses
  6.   { Scanners need to know about TTable objects. }
  7.   DBTables,
  8.   { Scanners need to know about table states. }
  9.   DB;
  10.  
  11. type
  12.  
  13.   {--------------------------------------------------------------
  14.     TTableScanner is the base abstact class for all table
  15.     scanner objects
  16.   }
  17.   TTableScanner = class( TObject )
  18.   private
  19.     { SourceTable is the table that will be scanned through. }
  20.     SourceTable: TTable;
  21.   public
  22.     { Execute performs the actual scanning of the SourceTable. }
  23.     procedure Execute; virtual; abstract;
  24.     constructor Create( ATable: TTable );
  25.   end;
  26.  
  27.   {--------------------------------------------------------------
  28.     TScannerAction is a procedural type. The TScannerAction
  29.     procedure will need to be defined by clients of the
  30.     TTableActionScanner object.
  31.   }
  32.  
  33.   TScannerAction = procedure( SourceTable: TTable );
  34.  
  35.   {--------------------------------------------------------------
  36.     TScannerCondition is another procedural type that will need
  37.     to be defined by clients of the TTableActionScanner object.
  38.     Its purpose is that of decideing whether the TScannerAction
  39.     is applicable or not to the current record in the
  40.     SourceTable. It is used only in conditional scanners.
  41.   }
  42.  
  43.   TScannerCondition = function( SourceTable: TTable ): Boolean ;
  44.  
  45.   {--------------------------------------------------------------
  46.     TScannerTrasferAction is yet another procedural type that
  47.     representing a procedure capable of transferring information
  48.     from a SourceTablle to a TargetTable.
  49.   }
  50.  
  51.   TScannerTransferAction = procedure(
  52.     SourceTable, TargetTable: TTable
  53.   );
  54.  
  55.   {--------------------------------------------------------------
  56.     The TTableActionScanner is capable of applying an Action
  57.     to all records of its SourceTable.
  58.   }
  59.   TTableActionScanner = class( TTableScanner )
  60.   public
  61.     {------------------------------------------------------------
  62.       The TTableActionScanner contains an Action field, the type
  63.       of which is the procedural type TScannerAction. This field
  64.       will need to be initialized with the actual action
  65.       action procedure. In other words, this fields "points" to
  66.       the action (procedure) to be performed on each record
  67.       of the SoruceTable.
  68.     }
  69.     Action: TScannerAction;
  70.     procedure Execute; override;
  71.     constructor Create(
  72.       ATable: TTable;
  73.       AnAction: TScannerAction
  74.     );
  75.   end;
  76.  
  77.   {--------------------------------------------------------------
  78.     The TTableConditionalActionScanner is capable of applying an
  79.     Action of all records of its SourceTalbe that pass the
  80.     Condition test.
  81.   }
  82.   TTableConditionalActionScanner = class( TTableActionScanner )
  83.   public
  84.     {------------------------------------------------------------
  85.       The TTableConditionalActionScanner is similar to the
  86.       TTableActionScanner, except that the action is
  87.       conditioned. Again, the actual condition is expressed
  88.       by a procedural type, the Condition field, which will have
  89.       to be defined by clients of this class.
  90.     }
  91.     Condition: TScannerCondition;
  92.     procedure Execute; override;
  93.     constructor Create(
  94.       ATable: TTable;
  95.       AnAction: TScannerAction;
  96.       ACondition: TScannerCondition
  97.     );
  98.   end;
  99.  
  100.   {--------------------------------------------------------------
  101.     The TTableTransferScanner is capable of transferring all
  102.     records from its SourceTable to its TargetTable.
  103.   }
  104.   TTableTransferScanner = class( TTableScanner )
  105.   private
  106.     TargetTable : TTable;
  107.   public
  108.     TransferAction: TScannerTransferAction;
  109.     procedure Execute; override;
  110.     constructor Create(
  111.       ASourceTable: TTable;
  112.       ATargetTable: TTable;
  113.       ATransferAction: TScannerTransferAction
  114.     );
  115.   end;
  116.  
  117.   {--------------------------------------------------------------
  118.     The TTableConditionalTransferScanner is capable of
  119.     transferring, from its SourceTable to its TargetTable, all
  120.     of the records that pass the TransferCondition.
  121.   }
  122.   TTableConditionalTransferScanner = class(TTableTransferScanner)
  123.   public
  124.     TransferCondition: TScannerCondition;
  125.     procedure Execute; override;
  126.     constructor Create(
  127.       ASourceTable: TTable;
  128.       ATargetTable: TTable;
  129.       ATransferAction: TScannerTransferAction;
  130.       ATransferCondition: TScannerCondition
  131.     );
  132.   end;
  133.  
  134. { UNIT IMPLEMENTATION SECTION
  135. ----------------------------------------------------------------}
  136.  
  137. implementation
  138.  
  139. { TTableScanner Constructor
  140. ----------------------------------------------------------------}
  141. constructor TTableScanner.Create( ATable: TTable );
  142. begin
  143.   inherited Create;
  144.   SourceTable := ATable;
  145. end;
  146.  
  147. { TTableActionScanner Constructor
  148. ----------------------------------------------------------------}
  149. constructor TTableActionScanner.Create(
  150.   ATable: TTable;
  151.   AnAction: TScannerAction
  152. );
  153. begin
  154.   inherited Create( ATable );
  155.   Action := AnAction;
  156. end;
  157.  
  158. { TTableConditionalActionScanner Constructor
  159. ----------------------------------------------------------------}
  160. constructor TTableConditionalActionScanner.Create(
  161.   ATable: TTable;
  162.   AnAction: TScannerAction;
  163.   ACondition: TScannerCondition
  164. );
  165. begin
  166.   inherited Create( ATable, AnAction );
  167.   Condition := ACondition;
  168. end;
  169.  
  170. { TTableTransferScanner Constructor
  171. ----------------------------------------------------------------}
  172. constructor TTableTransferScanner.Create(
  173.   ASourceTable: TTable;
  174.   ATargetTable: TTable;
  175.   ATransferAction: TScannerTransferAction
  176. );
  177. begin
  178.   inherited create( ASourceTable );
  179.   TargetTable := ATargetTable;
  180.   TransferAction := ATransferAction;
  181. end;
  182.  
  183. { TTableConditionalTransferScanner Constructor
  184. ----------------------------------------------------------------}
  185. constructor TTableConditionalTransferScanner.Create(
  186.   ASourceTable: TTable;
  187.   ATargetTable: TTable;
  188.   ATransferAction: TScannerTransferAction;
  189.   ATransferCondition: TScannerCondition
  190. );
  191. begin
  192.   inherited create(
  193.     ASourceTable,
  194.     ATargetTable,
  195.     ATransferAction
  196.   );
  197.   TransferCondition := ATransferCondition;
  198. end;
  199.  
  200. { TTableActionScanner.Execute Procedure
  201. ----------------------------------------------------------------}
  202. procedure TTableActionScanner.Execute;
  203. begin
  204.   SourceTable.DisableControls;
  205.   SourceTable.First;
  206.   while not SourceTable.Eof do
  207.   begin
  208.     Action( SourceTable ) ;
  209.     SourceTable.Next;
  210.   end;
  211.   if SourceTable.State = dsEdit then
  212.     SourceTable.Post;
  213.   SourceTable.EnableControls;
  214. end;
  215.  
  216. { TTableConditionalActionScanner.Execute Procedure
  217. ----------------------------------------------------------------}
  218. procedure TTableConditionalActionScanner.Execute;
  219. begin
  220.   SourceTable.DisableControls;
  221.   SourceTable.First;
  222.   while not SourceTable.Eof do
  223.   begin
  224.     if Condition( SourceTable ) then
  225.       Action( SourceTable ) ;
  226.     SourceTable.Next;
  227.   end;
  228.   if SourceTable.State = dsEdit then
  229.     SourceTable.Post;
  230.   SourceTable.EnableControls;
  231. end;
  232.  
  233. { TTableTransferScanner.Execute Procedure
  234. ----------------------------------------------------------------}
  235. procedure TTableTransferScanner.Execute;
  236. begin
  237.   SourceTable.DisableControls;
  238.   TargetTable.DisableControls;
  239.   SourceTable.First;
  240.   while not SourceTable.Eof do
  241.   begin
  242.     TransferAction( SourceTable, TargetTable ) ;
  243.     SourceTable.Next;
  244.   end;
  245.   if TargetTable.State in [dsEdit, dsInsert] then
  246.     TargetTable.Post;
  247.   if SourceTable.State = dsEdit then
  248.     SourceTable.Post;
  249.   TargetTable.EnableControls;
  250.   SourceTable.EnableControls;
  251. end;
  252.  
  253. { TTableConditionalTransferScanner.Execute Procedure
  254. ----------------------------------------------------------------}
  255. procedure TTableConditionalTransferScanner.Execute;
  256. begin
  257.   SourceTable.DisableControls;
  258.   TargetTable.DisableControls;
  259.   SourceTable.First;
  260.   while not SourceTable.Eof do
  261.   begin
  262.     if TransferCondition( SourceTable ) then
  263.       TransferAction( SourceTable, TargetTable ) ;
  264.     SourceTable.Next;
  265.   end;
  266.   if TargetTable.State in [dsEdit, dsInsert] then
  267.     TargetTable.Post;
  268.   if SourceTable.State = dsEdit then
  269.     SourceTable.Post;
  270.   TargetTable.EnableControls;
  271.   SourceTable.EnableControls;
  272. end;
  273.  
  274. end.
  275.